﻿using DBQLTV;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DAO
{
    public class LoaiDocGiaDAO
    {
        /// <summary>
        /// Lấy danh sách các loại độc giả
        /// </summary>
        /// <returns></returns>
        public List<string> GetListLoaiDocGia()
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                List<string> lResult = new List<string>();
                List<LOAIDOCGIA> lLoaiDocGia = entity.LOAIDOCGIAs.ToList();
                foreach (LOAIDOCGIA loaidocgia in lLoaiDocGia)
                {
                    lResult.Add(loaidocgia.TenLoaiDG);
                }
                return lResult;
            }
        }

        public bool InsertLoaiDocGia(string TenLoaiDocGia)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                LOAIDOCGIA loaiDG_temp = entity.LOAIDOCGIAs.FirstOrDefault(o => o.TenLoaiDG == TenLoaiDocGia);
                if (loaiDG_temp != null)
                    return false;
                LOAIDOCGIA loaidocgia = new LOAIDOCGIA();
                loaidocgia.TenLoaiDG = TenLoaiDocGia;

                entity.LOAIDOCGIAs.AddObject(loaidocgia);
                entity.SaveChanges();
                return true;
            }
        }
        public bool DeleteLoaiDocGia(string TenLoaiDG)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                LOAIDOCGIA loaiDG = entity.LOAIDOCGIAs.FirstOrDefault(o => o.TenLoaiDG.Equals(TenLoaiDG));
                if (loaiDG != null)
                {
                    entity.LOAIDOCGIAs.DeleteObject(loaiDG);
                    entity.SaveChanges();
                    return true;
                }
                return false;
            }
        }

        /// <summary>
        /// Cập nhật tên một loại độc giả
        /// </summary>
        /// <param name="TenLoaiDocGia"></param>
        /// <param name="TenLoaiDocGiaMoi"></param>
        public bool UpdateLoaiDocGia(string TenLoaiDocGia, string TenLoaiDocGiaMoi)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                LOAIDOCGIA loaidocgia = entity.LOAIDOCGIAs.FirstOrDefault(o => o.TenLoaiDG == TenLoaiDocGia);
                if (loaidocgia == null)
                    return false;
                loaidocgia.TenLoaiDG = TenLoaiDocGiaMoi;
                entity.SaveChanges();
                return true;
            }
        }

        public static LOAIDOCGIA GetLoaiDocGiaByName(string TenLoaiDocGia)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                LOAIDOCGIA loaiDocGia = entity.LOAIDOCGIAs.FirstOrDefault(o => o.TenLoaiDG == TenLoaiDocGia);
                return loaiDocGia;
            }
        }
        public string GetTenLoaiDocGiaByKey(int MaLoaiDG)
        {
            using (DBQUANLYTHUVIENEntities entity = new DBQUANLYTHUVIENEntities())
            {
                LOAIDOCGIA loaiDocGia = entity.LOAIDOCGIAs.FirstOrDefault(o => o.MaLoaiDG == MaLoaiDG);
                return loaiDocGia.TenLoaiDG;
            }
        }
    }
}
